package com.ljf.mvcssh.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import com.ljf.mvcssh.dao.BaseDao;
import com.ljf.mvcssh.util.Page;
import com.ljf.mvcssh.util.ReflectionUtils;
import com.ljf.mvcssh.entity.BaseEntity;

/*
 * 公共的dao的实现类
 * @author 林景锋
 * @date 2017年10月2日
 * @param <T>	实体
 */
@Repository("baseDao")
public class BaseDaoImpl<T> extends HibernateDaoSupport implements BaseDao<T> {

	@Resource(name = "sessionFactory") 
	public void setSuperSessionFactory(SessionFactory sessionFactory){
		super.setSessionFactory(sessionFactory);
	}

	/**
	 * 创建一个Class的对象来获取泛型的class
	 */

	private Class<T> clz;


	public BaseDaoImpl() {
		this.clz = ReflectionUtils.getSuperClassGenricType(getClass());

	}
	public BaseDaoImpl(final Class<T> clz){
		this.clz = clz;
	}
	public Class<T> getClz(){
		return clz;
	}
	@Override
	public void save(T t) {
		this.getHibernateTemplate().save(t);

	}

	@Override
	public void delete(int id) {
		this.getHibernateTemplate().delete(this.load(id));

	}

	@Override
	public void update(T t) {

		this.getHibernateTemplate().update(t);

	}

	@Override
	public T load(int id) {
		return this.getHibernateTemplate().load(getClz(), id);
	}

	@SuppressWarnings("unchecked")
	public T getUniqueResult(String hql, Object... args) {
		T t = null;
		Query query = this.getSession().createQuery(hql);
		for(int i=0;i<args.length;i++){
			query.setParameter(i, args[i]);
		}
		t = (T) query.uniqueResult();
		return t;
	}

	@Override
	public List<T> list(String hql, Object... args) {
		Query query = this.getSession().createQuery(hql);
		for(int i=0;i<args.length;i++){
			query.setParameter(i, args[i]);
		}
		List<T> list =query.list();
		return list;


	}

	//获取一张表的的全部数据
	@SuppressWarnings({"unchecked"})
	public List<T> getAllFromTable() {
		String hql = "from" + getClz().getSimpleName();
		Query query = this.getSession().createQuery(hql);
		List<T> list = query.list();
		return list;
	}

	/**
	 * 分页查询
	 */
	@SuppressWarnings("unchecked")
	@Override
	public boolean findForPage(Page<T> page) {
		Session session = this.getSession();
    	if(page==null){
    		return false;
    	}
    	//查询总数
    	String hql = "select count(*) from " + getClz().getSimpleName();
    	long total = (long)session.createQuery(hql).uniqueResult();
    	//设置分页总记录数，同时计算总页数
    	page.setTotal(total);
    	
    	//分页查询
    	hql = "from " + getClz().getSimpleName();
    	Query query = session.createQuery(hql);
    	//设置第一条记录的位置
    	query.setFirstResult((page.getPageNo()-1)*page.getOnePageNum());
    	//设置查询总记录数
    	query.setMaxResults(page.getOnePageNum());
    	List<T> list = query.list();
    	page.setDatas(list);
		return true;
	}

	// 通过条件进行分页查询
	@SuppressWarnings("unchecked")
	@Override
	public boolean findPageByCondition(Page<T> page, String condition) {
		Session session = this.getSession();
		if(page==null){
			return false;
		}
		//查询总数
		String hql = "select count(*) from "+getClz().getSimpleName() + condition;
		long total = (long) session.createQuery(hql).uniqueResult();
		//设置分页总记录数，同时计算总页数
		page.setTotal(total);
		
		//分页查询
		hql = "from " + getClz().getSimpleName() + condition;
		Query query = session.createQuery(hql);
		//设置第一条记录的位置
		query.setFirstResult((page.getPageNo()-1)*page.getOnePageNum());
		//设置查询总记录数
		query.setMaxResults(page.getOnePageNum());
		List<T> list = query.list();
		page.setDatas(list);
		return true;
	}

	//批量删除
	@Override
	public void batchDelete(List<Integer> ids) {
		for(int i=0;i<ids.size();i++){
			this.delete(ids.get(i));
		}
	}

	@Override
	public void saveOrUpdate(T t) {
		// TODO Auto-generated method stub

		this.getHibernateTemplate().saveOrUpdate(t);
	}

	
}
